package com.rhok.wthru_client;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.gcm.GCMRegistrar;
import com.rhok.wthru_client.servermsgs.ServerMessageOpaque;
import com.rhok.wthru_client.servermsgs.ServerMessageRegister;
import com.rhok.wthru_client.servermsgs.ServerMessageTask;

public class RegistrationActivity extends Activity {

	private static final String LOG_TOPIC = "WTHRU_RegistrationActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		GCMRegistrar.checkDevice(this);
		// Make sure the manifest was properly set - comment out this line
		// while developing the app, then uncomment it when it's ready.
		GCMRegistrar.checkManifest(this);
		
		registerToLocationChangedEvent();

		GlobalConfiguration gc = GlobalConfiguration
				.getInstance(getApplicationContext());
		if (gc.isRegistered()) {
			setContentView(R.layout.activity_main);
		} else {
			setContentView(R.layout.activity_registration);

			Button b = ((Button) this.findViewById(R.id.registerBtn));
			b.setOnClickListener(onRegisterListener);
		}
	}

	private void GCMRegistration() {
		Log.v(LOG_TOPIC, "GCMRegistration - called");
		
		final String regId = GCMRegistrar.getRegistrationId(this);
		if (regId.equals("")) {
			Log.v(LOG_TOPIC, "Not registered. Trying to register with SENDER_ID " + GlobalConfiguration.SENDER_ID);
			GCMRegistrar.register(this, GlobalConfiguration.SENDER_ID);
			Log.v(LOG_TOPIC, "After registration");
		} else {
			Log.v(LOG_TOPIC, "Already registered");
		}
	}

	private void registerToLocationChangedEvent() {

		LocationListener onLocationChnanged = new LocationListener() {

			private GlobalConfiguration gc = GlobalConfiguration
					.getInstance(getApplicationContext());

			@Override
			public void onStatusChanged(String provider, int status,
					Bundle extras) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onLocationChanged(Location location) {
				//
				gc.setLastKnownLocation(location);

			}
		};

		LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 500,
				onLocationChnanged, null);
		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 500,
				onLocationChnanged, null);
	}

	protected OnClickListener onRegisterListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String phoneNumber = ((EditText) RegistrationActivity.this
					.findViewById(R.id.phoneTxt)).getText().toString();
			phoneNumber = phoneNumber.replaceAll(" ", "");

			GlobalConfiguration globalConf = GlobalConfiguration.getInstance(getApplicationContext());
			globalConf.setLocalInfo(new BasicContactInfo("", phoneNumber), false);
			
			Log.d(LOG_TOPIC, "Phone is '" + phoneNumber + "', trying to register GCM");
			
			GCMRegistration();
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_registration, menu);
		return true;
	}

}
